/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dynamicconfigmanager

import (
	"errors"
	"reflect"
	"testing"

	"github.com/agiledragon/gomonkey/v2"
	"github.com/smartystreets/goconvey/convey"
	v1 "k8s.io/api/core/v1"

	"yuanrong.org/kernel/pkg/common/faas_common/k8sclient"
	commonTypes "yuanrong.org/kernel/pkg/common/faas_common/types"
	"yuanrong.org/kernel/pkg/functionscaler/types"
)

func TestHandleUpdateFunctionEvent(t *testing.T) {
	convey.Convey("HandleUpdateFunctionEvent", t, func() {
		defer gomonkey.ApplyMethod(reflect.TypeOf(&k8sclient.KubeClient{}), "CreateOrUpdateConfigMap",
			func(_ *k8sclient.KubeClient, c *v1.ConfigMap) error {
				return errors.New("CreateOrUpdateConfigMap configmap error")
			}).Reset()
		HandleUpdateFunctionEvent(&types.FunctionSpecification{
			ExtendedMetaData: commonTypes.ExtendedMetaData{DynamicConfig: commonTypes.DynamicConfigEvent{
				Enabled:       true,
				ConfigContent: []commonTypes.KV{{Name: "config1", Value: "value1"}},
			}},
		})
	})
}

func TestHandleDeleteFunctionEvent(t *testing.T) {
	convey.Convey("HandleDeleteFunctionEvent", t, func() {
		defer gomonkey.ApplyMethod(reflect.TypeOf(&k8sclient.KubeClient{}), "DeleteK8sConfigMap",
			func(_ *k8sclient.KubeClient, namespace string, configMapName string) error {
				return errors.New("delete configmap error")
			}).Reset()
		HandleDeleteFunctionEvent(&types.FunctionSpecification{})
	})
}
